from contextlib import contextmanager


@contextmanager
def my_open(file, mode):
    fp = open(file, mode)
    if fp:
        print("open file success")

    try:
        yield fp
    finally:
        fp.close()


with my_open("test2.txt", "w") as fp:
    fp.write("hello world")
